# -*- coding: utf-8 -*-
import time

import pyautogui

import jd.scene as scene
import jd.action as action
import pyperclip

# 位置坐标
position = {
    'search_text': (270, 150),
    'name_text': (350, 260),
    'phone_text': (400, 360),
    'room_no_text': (370, 590),
    'room_no_1_text': (410, 720),
    'address_input_text': (366, 232),
    'index_alert_close': (660, 151),
    'index_search_text': (320, 210),
    'city_select_area': {"left": 962, "top": 263, "width": 170, "height": 85},
    'app_close_area': {"left": 924, "top": 44, "width": 336, "height": 34},
    'duo_kai_qi_area': {"left": 883, "top": 400, "width": 800, "height": 640}
}

# 页面跳转路由
route = ['', '']

# 打开指定模拟器
def run_open_moniqi(name):
    area = position['duo_kai_qi_area']
    while True:
        has_img = action.has_img('lock.png', area)
        if has_img:
            break
        else:
            action.moveTo(420, 860)
            action.scroll(100)

    max_scroll_num = 10
    scroll_num = 0
    while True:
        if scroll_num > max_scroll_num:
            return -1
        res = action.get_text_position(name, area)
        if res is not None:
            x = area['left'] + res[0] + 610
            y = area['top'] + res[1]
            pyautogui.click(x, y)
            return 1
        else:
            action.moveTo(420, 860)
            action.scroll(-100)
            scroll_num += 1

# 关闭APP
def run_close_moniqi():
    action.click('close_moniqi', 4, 2)

# 打开APP
def run_open_app():
    while True:
        # 判断当前场景
        scene_now = scene.getscene(route)
        if scene_now == '模拟器':
            action.click('open_app')
            break

# # 打开APP
# def run_open_app(index):
#     scroll_num = math.ceil(index / 15)
#     for i in range(scroll_num):
#         action.moveTo(400, 850)
#         action.scroll(-100)
#         time.sleep(0.5)
#     time.sleep(1)
#     index = index - (math.floor(index / 15) * 15)
#     action.click('open_app', 5, -25, 1, index - 1)
#
# # 关闭APP
# def run_close_app():
#     action.click_img(action.get_main_gray(position['app_close_area']), 'close_app.png', 2, 2)
#     time.sleep(1)
#     for i in range(5):
#         action.moveTo(400, 850)
#         action.scroll(100)
#         time.sleep(0.5)

# 添加收货地址
def run_add_address(address):
    start = time.time()
    print('开始执行添加收货地址')
    while True:
        # 判断当前场景
        scene_now = scene.getscene(route)
        if scene_now == '首页':
            if route[0] == '新增收货地址':
                break
            else:
                action.click('close_advertisement')
                action.click('to_address_page')
        elif scene_now == '收货地址':
            action.click('to_add_address_page')
        elif scene_now == '新增收货地址':
            if route[0] == '收货地址' or route[0] == '':
                print('收货人', address['name'])
                pyperclip.copy(address['name'])
                action.paste(position['name_text'])
                pyperclip.copy(address['phone'])
                action.paste(position['phone_text'])
                pyperclip.copy(address['room_no'])
                action.paste(position['room_no_text'])
                action.paste(position['room_no_1_text'])
                action.click('to_select_area_page')
            elif route[0] == '所在地区':
                action.click('address_save')
        elif scene_now == '所在地区':
            next_step = False
            if route[0] == '新增收货地址' or route[0] == '':
                if action.get_text_position(address['city'], position['city_select_area']) is not None:
                    print('当前城市已选择')
                    next_step = True
                else:
                    action.click('to_select_city_page')
            elif route[0] == '选择城市':
                next_step = True

            if next_step:
                home = address['poi'] + address['sub_poi']
                print('小区名称: ' + home)
                pyperclip.copy(home)
                action.paste(position['address_input_text'], 1)
                road = address['district'] + address['road']
                print('道路名称: ' + road)
                res = action.choose_text(road, 1)
                if res:
                    print('选择地址成功')
                else:
                    print('兜底选择第一个地址')
                action.click('address_use')

        elif scene_now == '选择城市':
            province = address['province']
            city = address['city']
            res = action.choose_text(province)
            if res:
                res = action.choose_text(city)
                if res:
                    print('选择城市成功')
                else:
                    print('城市: ' + city + '选择失败')
            else:
                print('省份: ' + province + '选择失败')
            time.sleep(1)
    print('完成添加收货地址, 耗时: ', time.time() - start)

# 清空购物车
def run_clear_cart():
    start = time.time()
    print('开始执行清空购物车')
    while True:
        scene_now = scene.getscene(route)
        if scene_now == '首页':
            action.click_position(position['index_alert_close'][0], position['index_alert_close'][1])
            action.click('to_cart_page')
        elif scene_now == '购物车':
            # 点击编辑-》全选-》删除-》确认-》回首页
            result = action.click('cart_edit')
            if result:
                action.click('cart_all_select', 8)
                action.click('cart_delete')
                action.click('cart_delete_confirm')
            action.click('to_index_page')
            break
    print('完成清空购物车, 耗时: ', time.time() - start)

# 开始下单
def run_order(order):
    start = time.time()
    print('开始执行下单')
    goods_index = 0
    goods_count = len(order['goods_list'])
    while True:
        scene_now = scene.getscene(route)
        if scene_now == '首页':
            if route[0] == '支付成功':
                break
            else:
                action.click_position(position['index_alert_close'][0], position['index_alert_close'][1])
                action.click_position(position['index_search_text'][0], position['index_search_text'][1], 2)
        elif scene_now == '搜索':
            goods_name = order['goods_list'][goods_index]['name']
            print('添加商品: ', goods_name)
            pyperclip.copy(goods_name)
            action.paste(position['search_text'])
            action.click('search')
        elif scene_now == '商品列表':
            quantity = order['goods_list'][goods_index]['quantity']
            for i in range(quantity):
                action.click('add')
            goods_index += 1
            if goods_index >= goods_count:
                action.click('cart')
            else:
                action.click('back')
        elif scene_now == '购物车':
            action.click('settle')
    print('完成下单, 耗时: ', time.time() - start)